﻿namespace MyTRCP.Common.Models
{
    public class Location : BitWalkableObj
    {
        public int Latitude { get; set; }
        public int Longitude { get; set; }
        public short Altitude { get; set; }
        public short Speed { get; set; }
        public short Bearing { get; set; }
        public uint Timestamp { get; set; }
        public Location(byte[] data):base(data)
        {
        }

        public override BitWalker WriteToBitWalker(BitWalker bw)
        {
            return bw
                .WriteBits(Latitude, 24)
                .WriteBits(Longitude, 24)
                .WriteBits(Altitude, 16)
                .WriteBits(Speed, 16)
                .WriteBits(Bearing, 16)
                .WriteBits((int)Timestamp, 32);
        }

        public override BitWalker ReadFromBitWalker(BitWalker bw)
        {
            if (bw.ByteLen < 6)
                throw new ArgumentException("字节数组长度过小（6）", nameof(bw));
            Latitude = (int)bw.ReadBits(24);
            Longitude = (int)bw.ReadBits(24);
            if (bw.ByteLen >= 16)
            {
                Altitude = (short)bw.ReadBits(16);
                Speed = (short)bw.ReadBits(16);
                Bearing = (short)bw.ReadBits(16);
                Timestamp = bw.ReadBits(32);
            }
            else
            {
                Altitude = -32768;
                Speed = -1;
                Bearing = -1;
                Timestamp = 0;
            }
            return bw;
        }
    }
}